<?php

require("./Conn.php");

if (!isset($_POST['req'])) {
    die("No request provided.");
    
  //---------------------LLENANDO PRIVILEGIOS USUARIO ---------------------------------------------
} else if (filter_input(INPUT_POST, 'req') == "PrivilegiosUsuario") {
        
        $json = array("success" => false, "rowCount" => 0, "Elements" => Array());
        $user = filter_input(INPUT_POST, 'user_');
        
        if (!$conn) {
            $m = oci_error();
            trigger_error(htmlentities($m['message']), E_USER_ERROR);
        }
        $curs = oci_new_cursor($conn);
        $stid = oci_parse($conn, "begin PRIVILEGIOS(:cursbv,'".$user."'); end;");
        oci_bind_by_name($stid, ":cursbv", $curs, -1, OCI_B_CURSOR);
        oci_execute($stid);
        oci_execute($curs);  // Execute the REF CURSOR like a normal statement id 
        
        $i =0;
        while ($row = oci_fetch_array($curs, OCI_ASSOC+OCI_RETURN_NULLS)) {
            array_push($json['Elements'], $row);
            $i++;
        }

        if ($i > 0) {
       
            $json['success'] = true;
            $json['rowCount'] =$i;
        }

        print( json_encode($json));

}

//-------------------------LLENADO LOS ROLES DEL USUARIO--------------------------------------
    if (filter_input(INPUT_POST, 'req') == "RolesUsuario") {
         
        $json = array("success" => false, "rowCount" => 0, "Elements" => Array());
        $user = filter_input(INPUT_POST, 'user_');
        
        if (!$conn) {
            $m = oci_error();
            trigger_error(htmlentities($m['message']), E_USER_ERROR);
        }
        $curs = oci_new_cursor($conn);
        $stid = oci_parse($conn, "begin ROLESUSUARIO(:cursbv,'".$user."'); end;");
        oci_bind_by_name($stid, ":cursbv", $curs, -1, OCI_B_CURSOR);
        oci_execute($stid);
        oci_execute($curs);  // Execute the REF CURSOR like a normal statement id 
        
        $i =0;
        while ($row = oci_fetch_array($curs, OCI_ASSOC+OCI_RETURN_NULLS)) {
            array_push($json['Elements'], $row);
            $i++;
        }

        if ($i > 0) {
       
            $json['success'] = true;
            $json['rowCount'] =$i;
        }

        print( json_encode($json));
        
    
    }
    
//-------------LLENANDO OBJETOS DEL USUARIO----------------------
    if (filter_input(INPUT_POST, 'req') == "ObjetosUsuario") {
                    
        $json = array("success" => false, "rowCount" => 0, "Elements" => Array());
        $user = filter_input(INPUT_POST, 'user_');
        
        if (!$conn) {
            $m = oci_error();
            trigger_error(htmlentities($m['message']), E_USER_ERROR);
        }
        $curs = oci_new_cursor($conn);
        $stid = oci_parse($conn, "begin OBJETOS(:cursbv,'".$user."'); end;");
        oci_bind_by_name($stid, ":cursbv", $curs, -1, OCI_B_CURSOR);
        oci_execute($stid);
        oci_execute($curs);  // Execute the REF CURSOR like a normal statement id 
        
        $i =0;
        while ($row = oci_fetch_array($curs, OCI_ASSOC+OCI_RETURN_NULLS)) {
            array_push($json['Elements'], $row);
            $i++;
        }

        if ($i > 0) {
       
            $json['success'] = true;
            $json['rowCount'] =$i;
        }

        print( json_encode($json));
        
    
    }

//-----------------LLENADO CUOTAS DEL USUARIO---------------------------
    
     if (filter_input(INPUT_POST, 'req') == "CuotasUsuario") {
                    
        $json = array("success" => false, "rowCount" => 0, "Elements" => Array());
        $user = filter_input(INPUT_POST, 'user_');
        
        if (!$conn) {
            $m = oci_error();
            trigger_error(htmlentities($m['message']), E_USER_ERROR);
        }
        $curs = oci_new_cursor($conn);
        $stid = oci_parse($conn, "begin QUOTAS(:cursbv,'".$user."'); end;");
        oci_bind_by_name($stid, ":cursbv", $curs, -1, OCI_B_CURSOR);
        oci_execute($stid);
        oci_execute($curs);  // Execute the REF CURSOR like a normal statement id 
        
        $i =0;
        while ($row = oci_fetch_array($curs, OCI_ASSOC+OCI_RETURN_NULLS)) {
            array_push($json['Elements'], $row);
            $i++;
        }

        if ($i > 0) {
       
            $json['success'] = true;
            $json['rowCount'] =$i;
        }

        print( json_encode($json));
        
    
    }